package org.demo.db.meta.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * SqlServer -> 元数据信息
 * @author  
 * @date    2010-9-9
 * @file    org.loushang.db.meta.impl.SqlServerMetaImpl.java
 */
public class SqlServerMetaImpl extends CommonDbMetaImpl {
	/**
	 * 构造函数
	 * @param driver
	 * @param url
	 * @param user
	 * @param password
	 * @throws Exception
	 */
	public SqlServerMetaImpl(String driver,String url,
		String user,String password)throws Exception{
		super(driver,url,user,password);
	}
	/**
	 * 构造函数
	 * @param conn
	 */
	public SqlServerMetaImpl(Connection conn)throws SQLException{
		super(conn);
	}	
	/**
	 * 获取当前连接的 schema
	 * @return
	 */
	@Override
	public String getCurrentSchema(){
		String schema = null;
		PreparedStatement prst = null;
		
		try {
			String sql = "SELECT DEFAULT_SCHEMA_NAME " 
				       + "FROM SYS.DATABASE_PRINCIPALS WHERE NAME=?";
			prst = conn.prepareStatement(sql);
			prst.setString(1, dbmd.getUserName());
			ResultSet rs = prst.executeQuery();
			if (rs.next()){
				schema = rs.getString(1);
			}
			// 关闭游标
			rs.close();
		} catch (Exception e){
			// Ignore.
		} finally{
			try {if (prst != null){prst.close();}} catch (Exception e){}
		}
		
		return schema;
	}
}
